# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_wx_basever=3.2

_realname=mediainfo 
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-gui")
pkgver=25.04
pkgrel=1
pkgdesc="Supplies technical and tag information about media files (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://mediaarea.net"
msys2_repository_url='https://github.com/MediaArea/MediaInfo'
msys2_references=(
  "cpe: cpe:/a:mediaarea:mediainfo"
)
license=("spdx:BSD-2-Clause")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-autotools"
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-libmediainfo"
  "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw"
  "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-gtk3"
)
depends=(
  "${MINGW_PACKAGE_PREFIX}-dlfcn"
  "${MINGW_PACKAGE_PREFIX}-gcc-libs"
)
source=("https://mediaarea.net/download/source/${_realname}/${pkgver}/${_realname}_${pkgver}.tar.xz"
        001-missing-rc-mingw.patch)
sha256sums=('4b2553fe9104332d3baca5fe61b6b87af4d493108c5b863801cdb0a4826a33ae'
            '116567243123b6d18e7e25c79e268be858301a7c269de213fc57e6dfec3ae203')
noextract=("${_realname}_${pkgver}.tar.xz")

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  echo "Extracting ${_realname}_${pkgver}.tar.xz ..."
  tar -xJf ${_realname}_${pkgver}.tar.xz || true

  cd "${srcdir}/MediaInfo"

  apply_patch_with_msg \
    001-missing-rc-mingw.patch

  cd "${srcdir}/MediaInfo/Project/GNU/CLI"

  ./autogen.sh

  cd "${srcdir}/MediaInfo/Project/GNU/GUI"

  ./autogen.sh
}

build() {
  # Build CLI interface
  cd "${srcdir}/MediaInfo/Project/GNU/CLI"

  CXXFLAGS="-D_UNICODE" \
  ./configure \
    --prefix="${MINGW_PREFIX}" \
    --host="${MINGW_CHOST}" \
    --build="${MINGW_CHOST}" \
    --enable-shared \
    --disable-static \
    --with-dll


  make

  # Build GUI interface
  cd "${srcdir}/MediaInfo/Project/GNU/GUI"

  CXXFLAGS="-D_UNICODE" \
  ./configure \
    --prefix="${MINGW_PREFIX}" \
    --host="${MINGW_CHOST}" \
    --build="${MINGW_CHOST}" \
    --enable-shared \
    --disable-static \
    --with-wxwidgets \
    --with-wx-gui \
    --with-dll

  make
}

package_mediainfo() {
  pkgdesc="Supplies technical and tag information about media files (CLI Interface) (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-libmediainfo")

  cd "${srcdir}/MediaInfo/Project/GNU/CLI"

  make DESTDIR="${pkgdir}" install

  install -vDm 644 "${srcdir}/MediaInfo/LICENSE" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/$_realname/"
  install -vDm 644 "${srcdir}/MediaInfo/"{History_CLI.txt,README.md} -t "${pkgdir}${MINGW_PREFIX}/share/doc/$_realname/"
}

package_mediainfo-gui() {
  pkgdesc="Supplies technical and tag information about media files (GUI Interface) (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-libmediainfo"
           "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-gtk3")

  cd "${srcdir}/MediaInfo/Project/GNU/GUI"

  make DESTDIR="${pkgdir}" install

  install -vDm 644 "${srcdir}/MediaInfo/LICENSE" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-gui/"
  install -vDm 644 "${srcdir}/MediaInfo/"{History_GUI.txt,README.md} -t "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}-gui/"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
